% $Id: parse_csv.m 21 2008-11-25 14:26:51Z Jie.Bao $

function parse_csv

close all

% =============== PARAMETERIZATION =================
subjectToPlot = 11;                 % subject number
dim = 1;                            % plot dimension
file_type = 'test';                 % log file type, either 'test' or
                                    % 'training'

% plot parameterization
plot_params.point_size = 30;        % marker size of the scatter plot
plot_params.font_size = 16;         % font size of axis labels

% csv file name with the absolute path
filename = 'c:\Dokumente und Einstellungen\boggie\Desktop\data\gregory\Muster_ArcGIS_output.csv';

% ==================================================

[subject_str, ...
 event_name, ...
 time_str, ...
 area_str, ...
 detour_str, ...
 x_snapped_str, ...
 y_snapped_str, ...
 distance_str] = textread(filename, ...
    ['%*s%*s%*s%*s%*s%s%*s%*s%*s%*s%s%*s'...
    '%s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s%s%s%s%s'],...
    'delimiter', ';', 'headerlines', 1);

% convert numerical data to english format, mptoolbox required
distance = zeros(length(distance_str), 1);
y_snapped = zeros(length(y_snapped_str), 1);
x_snapped = zeros(length(x_snapped_str), 1);
time = zeros(length(time_str), 1);
area = zeros(length(area_str), 1);
subject = zeros(length(subject_str), 1);
detour = zeros(length(detour_str), 1);
for i = 1:length(distance_str)
    distance(i) = mp(str2double(regexprep(distance_str(i), ',', '.')));
    y_snapped(i) = mp(str2double(regexprep(y_snapped_str(i), ',', '.')));
    x_snapped(i) = mp(str2double(regexprep(x_snapped_str(i), ',', '.')));
    time(i) = mp(str2double(regexprep(time_str(i), ',', '.')));
    area(i) = mp(str2double(regexprep(area_str(i), ',', '.')));
    subject(i) = mp(str2double(regexprep(subject_str(i), ',', '.')));
    detour(i) = mp(str2double(regexprep(detour_str(i), ',', '.')));
end

% create a data matrix that has all the information
num_data = horzcat(time, x_snapped, y_snapped, distance, subject, area, detour);

% make the figure window fill the whole screen
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 scrsz(3) scrsz(4)])

% plotting
if length(subjectToPlot) == 1
    [event_name, num_data, order_by_name, unique_names] = ...
        getOneSubject(subjectToPlot, file_type, event_name, num_data);
    
    if dim == 1
        plot1D(num_data, order_by_name, unique_names, subjectToPlot, ...
               file_type, plot_params);
    elseif dim == 2
        plot2D(num_data, order_by_name, unique_names, subjectToPlot, ...
               file_type, plot_params);
    elseif dim == 3
        plot3D(num_data, order_by_name, unique_names, subjectToPlot, ...
               file_type, plot_params);
    end
    
else
    y_max = 0;
    ax = zeros(length(subjectToPlot), 1);
    original_event_name = event_name;
    original_num_data = num_data;

    for i = 1:length(subjectToPlot)
        [event_name, num_data, order_by_name, unique_names] = ...
            getOneSubject(subjectToPlot(i), file_type, ...
            original_event_name, original_num_data);
        
        subplot(length(subjectToPlot), 1, i);
        ax(i) = gca;
        
        if dim == 1
            plot1D(num_data, order_by_name, unique_names, ...
                       subjectToPlot(i), ...
                       file_type, plot_params);
        elseif dim == 2 
            plot2D(num_data, order_by_name, unique_names, ...
                       subjectToPlot(i), ...
                       file_type, plot_params);
               
            if y_max < max(num_data(:,1))
                y_max = max(num_data(:,1));
            end
            
        elseif dim == 3
            plot3D(num_data, order_by_name, unique_names, ...
                       subjectToPlot(i), ...
                       file_type, plot_params);
        end
    end
    
    % reset the ylim, in order to make all the subplots have the same scale
    for i = 1:length(ax)
        if dim == 2
            set(ax(i), 'ylim', [0 y_max]);
        end
    end
        
end
   

end


% ============================================================
function [event_name_cut, ...
          num_data_cut, ...
          order_by_name, ...
          unique_names] = getOneSubject(subjectToPlot, ...
                                         file_type, ...
                                         event_name, ...
                                         num_data)
%GETONESUBJECT cuts the original data matrix to a small one that only 
%              contains the subject(s) to be plotted, and also sorts 
%              all events by their names
%
% Usage: [event_name_cut, num_data_cut, order_by_name, unique_names] = ...
%           getOneSubject(subjectToPlot, file_type, event_name, num_data);
%
% Input: subjectToPlot   the number of the subject to be plotted 
%        file_type       log file type, either 'test' or 'training'
%        event_name      the original array of event names
%        num_data        the original data matrix 
%
% Output: event_name_cut   the cut array of event names
%         num_data_cut     the cut data matrix  
%         order_by_name    a cell array of event indices sorted by the 
%                          event name
%         unique_names     a unique set of event names
                                     
    % cut out the vectors to be plotted
    if strcmp(file_type, 'training')
        event_name_cut = event_name(num_data(:,5)==subjectToPlot & ...
            num_data(:,6)==1);
        num_data_cut = num_data(num_data(:,5)==subjectToPlot & ...
            num_data(:,6)==1, :);
    elseif strcmp(file_type, 'test')
        event_name_cut = event_name(num_data(:,5)==subjectToPlot & ...
            num_data(:,6)==2);
        num_data_cut = num_data(num_data(:,5)==subjectToPlot & ...
            num_data(:,6)==2, :);
    end

    % sort by event names
    unique_names = unique(event_name_cut);
    order_by_name = cell(length(unique_names), 1);
    for i = 1:length(unique_names)
        order_by_name{i} = find(strcmp(event_name_cut, unique_names{i}));
    end
    
end

% =======================================
function p = plot1D(num_data, ...
                order_by_name, ...
                unique_names, ...
                subjectToPlot, ...
                file_type, ...
                plot_params)
%PLOT1D generates a 1-D linear representation of the route of one subject,
%       clickableLegend.m required
%
% Usage: p = plot1D(num_data, order_by_name, unique_names, subjectToPlot, ...
%                file_type, plot_params)
%
% Input: num_data        the cut data matrix 
%        order_by_name   a cell array of event indices sorted by the 
%                        event name
%        unique_names    a unique set of event names
%        subjectToPlot   the number of the subject to be plotted 
%        file_type       log file type, either 'test' or 'training'
%        plot_params     plot parameters predefined by the user, e.g.
%                        point size, font size, etc.
%
% Output: p              an array of plot handles            
            
    p = zeros(length(order_by_name), 1);
    hold on;
    for i = 1:length(order_by_name)
        p(i) = scatter(num_data(order_by_name{i}, 4), ...
            subjectToPlot*ones(length(order_by_name{i}), 1), ...
            plot_params.point_size, 'filled');
    end
    hold off;
    
    if strcmp(file_type, 'training')
        xlim([0 210]);
    elseif strcmp(file_type, 'test')
        xlim([0 780]);
    end
    
    set(gca, 'YTick', subjectToPlot-1:subjectToPlot+1, ...
        'YLim', [subjectToPlot-1.5 subjectToPlot+1.5]);
    
    xlabel('distance (m)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    ylabel('subject number', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    file_type(1) = upper(file_type(1));
    title([file_type ' (subject ' num2str(subjectToPlot) ')'], ...
        'FontWeight', 'Bold', 'FontSize', plot_params.font_size+4);
    
    unique_names = regexprep(unique_names, '_', '\\_');
    clickableLegend(unique_names, 'Location', 'NorthEastOutside');
    
end

% =======================================
function p = plot2D(num_data, ...
                order_by_name, ...
                unique_names, ...
                subjectToPlot, ...
                file_type, ...
                plot_params)
%PLOT2D plots the relative time against the 1-D linear representation of 
%       the route of one subject, clickableLegend.m required
%
% Usage: p = plot2D(num_data, order_by_name, unique_names, subjectToPlot, ...
%                file_type, plot_params)
%
% Input: num_data        the cut data matrix 
%        order_by_name   a cell array of event indices sorted by the 
%                        event name
%        unique_names    a unique set of event names
%        subjectToPlot   the number of the subject to be plotted 
%        file_type       log file type, either 'test' or 'training'
%        plot_params     plot parameters predefined by the user, e.g.
%                        point size, font size, etc.
%
% Output: p              an array of plot handles            
            
    p = zeros(length(order_by_name), 1);
    hold on;
    for i = 1:length(order_by_name)
        p(i) = scatter(num_data(order_by_name{i}, 4), ...
            num_data(order_by_name{i}, 1), ...
            plot_params.point_size, 'filled');
    end
    hold off;
    
    if strcmp(file_type, 'training')
        xlim([0 210]);
    elseif strcmp(file_type, 'test')
        xlim([0 780]);
    end
    
    xlabel('distance (m)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    ylabel('time (s)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    file_type(1) = upper(file_type(1));
    title([file_type ' (subject ' num2str(subjectToPlot) ')'], ...
        'FontWeight', 'Bold', 'FontSize', plot_params.font_size+4);
    
    unique_names = regexprep(unique_names, '_', '\\_');
    clickableLegend(unique_names, 'Location', 'NorthEastOutside');
    
end

% =======================================
function plot3D(num_data, ...
                order_by_name, ...
                unique_names, ...
                subjectToPlot, ...
                file_type, ...
                plot_params)
%PLOT3D plots the relative time on top of the route of one subject,
%       clickableLegend.m required
%
% Usage: plot3D(num_data, order_by_name, unique_names, subjectToPlot, ...
%               file_type, plot_params)
%
% Input: num_data        the cut data matrix 
%        order_by_name   a cell array of event indices sorted by the 
%                        event name
%        unique_names    a unique set of event names
%        subjectToPlot   the number of the subject to be plotted 
%        file_type       log file type, either 'test' or 'training'
%        plot_params     plot parameters predefined by the user, e.g.
%                        point size, font size, etc.
            
    colors = {'y', 'm', 'c', 'r', 'g', 'b', 'k'};
            
    %stem3([],[],[]);
    hold on;
    for i = 1:length(order_by_name)
        stem3(num_data(order_by_name{i}, 2), ...
            num_data(order_by_name{i}, 3), ...
            num_data(order_by_name{i}, 1), ...
            'fill', ...
            'Color', colors{mod(i, 7) + 1});
    end
    hold off;
    box off;
    grid on;
    view([50 74]);
      
    xlabel('x\_snapped (m)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    ylabel('y\_snapped (m)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    zlabel('time (s)', 'FontWeight', 'Bold', ...
        'FontSize', plot_params.font_size);
    file_type(1) = upper(file_type(1));
    title([file_type ' (subject ' num2str(subjectToPlot) ')'], ...
        'FontWeight', 'Bold', 'FontSize', plot_params.font_size+4);
    
    unique_names = regexprep(unique_names, '_', '\\_');
    clickableLegend(unique_names, 'Location', 'NorthEastOutside');
    
end

% =======================================
function generate_legend(p, legend_names)
%GENERATE_LEGEND replaces the legend in order to fit them into the figure, 
%                probably OBSOLETE

    legend_names = regexprep(legend_names, '_', '\\_');

    hLegend = zeros(1, length(legend_names));

    bottom_left = get(get(p(1), 'Parent'), 'Position');
    top_left = [bottom_left(1) bottom_left(2)+bottom_left(4) ...
        bottom_left(3) bottom_left(4)];
    
    for n = 1:length(legend_names)

        ax = axes('visible', 'off');
        hLegend(n) = clickableLegend(ax, p(n), legend_names{n}, ...
            'Location', 'NorthWestOutside', ...
            'Orientation', 'horizontal');
        legend(ax, 'boxoff');
        set(ax, 'NextPlot', 'add');

    end

    original_pos = get(hLegend(1), 'position');
    set(hLegend(1), 'Position', [top_left(1) top_left(2)-0.03 ...
        original_pos(3) original_pos(4)]);
    original_pos = get(hLegend(1), 'position');
    pos = original_pos;

    for n = 2:length(legend_names)
        current_pos = get(hLegend(n), 'position');

        if mod(n, 3) == 1
            pos = [original_pos(1), ...
                original_pos(2)-floor(n/3)*original_pos(4), ...
                current_pos(3), current_pos(4)];
            set(hLegend(n), 'position', pos);
        else
            pos = [pos(1)+pos(3) pos(2) current_pos(3) current_pos(4)];
            set(hLegend(n), 'position', pos);
        end
    end

end



